﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AutoAdsParser
{
    class AutoAdItem
    {
        public int Id;
        public string Name;
        public int Year;
        public int Price;
        public string Contacts;
        public DateTime CreatedDate;
        public string Url;
        public int CountOfViews;
        public bool Sold;
        public int QuestionsCount;
        private static object _lock=new object();

        public int GetDiffDays()
        {
            return (int)(new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day) - CreatedDate).TotalDays;
        }

        public void Save() 
        {
            lock (_lock)
            {
                var model = new AutoAdEntities();
                var autoAd = model.AutoAd.FirstOrDefault(i => i.AdId == Id);
                if (autoAd == null)
                {
                    var newId = 1;
                    if (model.AutoAd.Any())
                        newId = model.AutoAd.Max(i => i.Id) + 1;
                    autoAd = new AutoAd()
                    {
                        Id = newId,
                        CreateDate = CreatedDate,
                        FullAdUrl = Url,
                        Contacts = Contacts,
                        Title = Name,
                        AdId = Id,
                        Price = Price,
                        Year = Year,
                        QuestionsCount = QuestionsCount

                    };
                    model.AddToAutoAd(autoAd);
                    model.SaveChanges();
                }
                else
                {
                    if (QuestionsCount>0)
                        autoAd.QuestionsCount = QuestionsCount;
                    if (autoAd.Price != Price)
                    {
                        var newHistory = new AdHistory()
                        {
                            Id = model.AdHistory.Any() ? model.AdHistory.Max(i => i.Id) : 1,
                            AutoAdId = autoAd.Id,
                            PropertyChanged = "price",
                            Value = autoAd.Price.ToString(),
                            CreateDate = DateTime.Now
                        };
                        model.AddToAdHistory(newHistory);
                        autoAd.Price = Price;
                        autoAd.PriceChanged = true;
                        model.SaveChanges();
                    }
                    if (autoAd.Sold.HasValue && autoAd.Sold != Sold)
                    {
                        var newHistory = new AdHistory()
                        {
                            Id = model.AdHistory.Any() ? model.AdHistory.Max(i => i.Id) : 1,
                            AutoAdId = autoAd.Id,
                            PropertyChanged = "sold",
                            Value = autoAd.Sold.ToString(),
                            CreateDate = DateTime.Now
                        };
                        model.AddToAdHistory(newHistory);
                        autoAd.Sold = Sold;
                        model.SaveChanges();
                    }
                }
            }

        }
    }
}
